package net.kleinhenz.norabase.client;

import net.kleinhenz.norabase.shared.model.Champion;
import net.kleinhenz.norabase.shared.model.Equipment;
import net.kleinhenz.norabase.shared.model.Relic;
import net.kleinhenz.norabase.shared.model.Rune;
import net.kleinhenz.norabase.shared.model.Spell;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;

public class FrontCard extends AbsolutePanel {

	private Image cardImg = null;
	private Image rarityImg = null;
	private Image borderImg = null;
	private Label nameLabel = null;
	private Label noraLabel = null;
	private Label attackLabel = null;
	private Label speedLabel = null;
	private Label rangeLabel = null;
	private Label defenseLabel = null;	
	private Label hpLabel = null;
	private Image statsIconsImg = null;
	
	public FrontCard() {

		createLayout();
	}
	
	private void createLayout() {

		setSize("310px", "390px");
		
		cardImg = new Image(PoxNoraURL.card(Rune.CHAMPION, 934));
		cardImg.getElement().getStyle().setProperty("zIndex", "100");
		add(cardImg, 20, 25);
				
		rarityImg = new Image();
		rarityImg.getElement().getStyle().setProperty("zIndex", "200");
		add(rarityImg, 0, 0);
		
		borderImg = new Image();
		borderImg.getElement().getStyle().setProperty("zIndex", "300");
		add(borderImg, 0, 0);

		statsIconsImg  = new Image();
		statsIconsImg.getElement().getStyle().setProperty("zIndex", "400");
		add(statsIconsImg, 0, 0);
		
		nameLabel = new Label();
		nameLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		nameLabel.setStyleName("cardTitle");
		nameLabel.getElement().getStyle().setProperty("zIndex", "500");
		add(nameLabel, 0, 13);
		nameLabel.setSize("312px", "23px");
		
		noraLabel = new Label();
		noraLabel.setStyleName("noraCost");
		noraLabel.getElement().getStyle().setProperty("zIndex", "500");
		add(noraLabel, 264, 11);
		
		attackLabel = new Label();
		attackLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		attackLabel.setStyleName("stats");
		attackLabel.getElement().getStyle().setProperty("zIndex", "500");
		add(attackLabel, 50, 369);
		attackLabel.setSize("40px", "20px");
		
		speedLabel = new Label();
		speedLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		speedLabel.setStyleName("stats");
		speedLabel.getElement().getStyle().setProperty("zIndex", "500");
		add(speedLabel, 90, 369);
		speedLabel.setSize("42px", "20px");

		rangeLabel = new Label();
		rangeLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		rangeLabel.setStyleName("stats");
		rangeLabel.getElement().getStyle().setProperty("zIndex", "500");
		add(rangeLabel, 135, 369);
		rangeLabel.setSize("40px", "20px");

		defenseLabel = new Label();
		defenseLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		defenseLabel.setStyleName("stats");
		defenseLabel.getElement().getStyle().setProperty("zIndex", "500");
		add(defenseLabel, 179, 369);
		defenseLabel.setSize("40px", "20px");

		hpLabel = new Label();
		hpLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		hpLabel.setStyleName("stats");
		hpLabel.getElement().getStyle().setProperty("zIndex", "500");
		add(hpLabel, 220, 369);
		hpLabel.setSize("41px", "20px");
	}

	public void loadRune(Rune rune) {

		if (rune instanceof Champion)
			cardImg.setUrl(PoxNoraURL.card(Rune.CHAMPION, rune.getId()));
		else if (rune instanceof Equipment)
			cardImg.setUrl(PoxNoraURL.card(Rune.EQUIPMENT, rune.getId()));
		else if (rune instanceof Spell)
			cardImg.setUrl(PoxNoraURL.card(Rune.SPELL, rune.getId()));
		else if (rune instanceof Relic)
			cardImg.setUrl(PoxNoraURL.card(Rune.RELIC, rune.getId()));

		rarityImg.setUrl(PoxNoraURL.rarityBorder(rune.getRarity()));
		borderImg.setUrl(PoxNoraURL.border(PoxNoraURL.FACTION.get(rune.getFaction()), 0));

		if (rune instanceof Champion) {
			statsIconsImg.setUrl(PoxNoraURL.statIcons());
		}
		
		nameLabel.setText(rune.getName());
		noraLabel.setText("" + rune.getNora());
		
		if (rune instanceof Champion) {
			Champion c = (Champion)rune;
			attackLabel.setText("" + c.getDmg());
			attackLabel.setVisible(true);
			speedLabel.setText("" + c.getSpd());
			speedLabel.setVisible(true);
			String rng = null;
			
			if (c.getMinrng()==c.getMaxrng())
				rng = "" + c.getMinrng();
			else
				rng = c.getMinrng() + "-" + c.getMaxrng();
			
			rangeLabel.setText(rng);
			rangeLabel.setVisible(true);
			defenseLabel.setText("" + c.getDef());	
			defenseLabel.setVisible(true);
			hpLabel.setText("" + c.getHp());
			hpLabel.setVisible(true);
			statsIconsImg.setVisible(true);
		} else if (rune instanceof Relic) {
			Relic r = (Relic)rune;
			hpLabel.setText("" + r.getHp());
			hpLabel.setVisible(true);
		} else {
			attackLabel.setVisible(false);
			speedLabel.setVisible(false);
			rangeLabel.setVisible(false);
			defenseLabel.setVisible(false);
			hpLabel.setVisible(false);			
			statsIconsImg.setVisible(false);
		}		
	}
}
